package com.qianfeng.smsplatform.strategy.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.constants.RabbitMqConsants;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import com.qianfeng.smsplatform.strategy.cache.LocalFilterOrderCache;
import com.qianfeng.smsplatform.strategy.feign.CacheService;
import com.qianfeng.smsplatform.strategy.filters.FilterChian;
import com.qianfeng.smsplatform.strategy.service.FilterManagerInterface;
import com.qianfeng.smsplatform.strategy.service.PushFailReportMessae;
import com.qianfeng.smsplatform.strategy.service.PushMessageToGatewayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by jackiechan on 2021/7/23/20:26
 *
 * @Author jackiechan
 */
@Service
public class FilterManagerImplLocalCache implements FilterManagerInterface {


    private LocalFilterOrderCache localFilterOrderCache;
    @Autowired
    public void setLocalFilterOrderCache(LocalFilterOrderCache localFilterOrderCache) {
        this.localFilterOrderCache = localFilterOrderCache;
    }


    private CacheService cacheService;

    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }


    private PushMessageToGatewayService pushMessageToGatewayService;

    @Autowired
    public void setPushMessageToGatewayService(PushMessageToGatewayService pushMessageToGatewayService) {
        this.pushMessageToGatewayService = pushMessageToGatewayService;
    }

    private Map<String, FilterChian> filterChianMap;

    @Autowired// Spring会将指定类型的对象保存到一个map中并给我们设置过来, map的key就是对象的id
    public void setFilterChianMap(Map<String, FilterChian> filterChianMap) {
        this.filterChianMap = filterChianMap;
    }

    private PushFailReportMessae pushFailReportMessae;
    @Autowired
    public void setPushFailReportMessae(PushFailReportMessae pushFailReportMessae) {
        this.pushFailReportMessae = pushFailReportMessae;
    }


    @Override
    public void dealSms(Standard_Submit standard_submit) {
        //现在我们需要做的就是从缓存中获取到我们需要执行的过滤器以及顺序,然后挨个执行
        List<String> filterOrderList = localFilterOrderCache.getFilterOrderList();//获取到我们需要执行的过滤器以及顺序,本次我们的保存的位置是在redis以及本地缓存
        if (filterOrderList != null) {
            //遍历 获取到每一个需要执行的过滤器的名字
            for (String filterName : filterOrderList) {
                FilterChian filterChian = filterChianMap.get(filterName);
                if (filterChian != null) {
                    boolean dealSms = filterChian.dealSms(standard_submit);
                    if (!dealSms) {
                        System.err.println("过滤器拦截了操作");
                        //返回状态报告告诉客户,失败了
                        Standard_Report report = new Standard_Report();
                        report.setState(2);
                        report.setMobile(standard_submit.getDestMobile());
                        report.setErrorCode(standard_submit.getErrorCode());
                        report.setDescription(standard_submit.getDescription());
                        report.setClientID(standard_submit.getClientID());
                        report.setSrcID(standard_submit.getSrcSequenceId());
                        pushFailReportMessae.pushFailReport(report);
                        return;
                    }
                }
            }
            //代表所有的过滤器执行完成并且没有异常,添加限流次数, 增加计费
            long currentTimeMillis = System.currentTimeMillis();
            cacheService.add2Zset(CacheConstants.CACHE_PREFIX_SMS_LIMIT + standard_submit.getClientID() + standard_submit.getDestMobile(), currentTimeMillis + "", currentTimeMillis);
            //发送短信到网关, 到哪个网关我们需要确定, 根据用户选择的自己的网关的id 拼接我们的队列的名字
            String channelid = cacheService.hGet(CacheConstants.CACHE_PREFIX_ROUTER + standard_submit.getClientID(), "channelid");
            pushMessageToGatewayService.pushMessage2GateWay(RabbitMqConsants.TOPIC_SMS_GATEWAY + channelid, standard_submit);//将数据发送到制定的网关
        }
    }
}
